<?php
/**
 * Created by PhpStorm.
 * User: wangan
 * Date: 2017/12/29
 * Time: 12:59
 */

namespace app\common\lib;

use PHPExcel_IOFactory;
use PHPExcel;
use PHPExcel_Cell;
use think\Exception;

class Excel2Array
{
    /**
     * 读取excel $filename 路径文件名 $encode 返回数据的编码 默认为utf8
     *以下基本都不要修改
     */
    public function read($filename,$encode='utf-8'){

       /* $PHPReader = new PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($file_temp)) {
            $PHPReader = new PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($file_temp)) {
                echo 'no Excel';
            }
        }*/

        $objReader = PHPExcel_IOFactory::createReader('Excel5');
        if(!$objReader->canRead($filename)){
            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
            if (!$objReader->canRead($filename)) {
                throw new Exception('无法识别该excle文件！');
            }
        }
        $objReader->setReadDataOnly(true);
        $objPHPExcel = $objReader->load($filename);
        $objWorksheet = $objPHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();

        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
        $excelData = array();
        for ($row = 1; $row <= $highestRow; $row++) {
            for ($col = 0; $col < $highestColumnIndex; $col++) {
                $excelData[$row][] =(string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
            }
        }
        return $excelData;
    }

    /* 导出excel函数*/
    public function write($data,$name='Excel'){
/*        include_once(IWEB_PATH.'core/util/PHPExcel.php');
        error_reporting(E_ALL);
        date_default_timezone_set('Europe/London');*/
        $objPHPExcel = new PHPExcel();

        /*以下是一些设置 ，什么作者  标题啊之类的*/
        $objPHPExcel->getProperties();
           /* ->setCreator("转弯的阳光")
            ->setLastModifiedBy("转弯的阳光")
            ->setTitle("数据EXCEL导出")
            ->setSubject("数据EXCEL导出")
            ->setDescription("备份数据")
            ->setKeywords("excel")
            ->setCategory("result file");*/
        /*以下就是对处理Excel里的数据， 横着取数据，主要是这一步，其他基本都不要改*/
        $row =1 ;

        foreach($data as $k => $v){
            $col = 'A';
            foreach ($v as $kk => $vv){
                $objPHPExcel->setActiveSheetIndex(0)//
                ->setCellValue($col.$row, $vv);
                $col++;
            }
            $row++;
        }

        $objPHPExcel->getActiveSheet()->setTitle('sheet0');
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

        $file_name = '错误'.uniqid().time().'.xlsx';
        $str_path = '\\tmp\\errorExcel'.'\\'.$file_name;
        if (!is_dir(APP_ROOT.$str_path)){
            mkdir(APP_ROOT.$str_path,0777,true);
        }
        chmod(APP_ROOT.$str_path, 0777);
        $objWriter->save($str_path);
        return $str_path;
    }
}